﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using System.Data;
using Think.Lenovo.Com.Models.Material;

namespace Think.Lenovo.Com.DataAccess
{
    public class productClassInfo
    {
        #region

        /// <summary>
        /// 获取ClassId的包含菜单列表
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public DataSet GetClassListByClassId(string ClassId)
        {

            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ClassList,ClassTj from tbl_product_class");
            strSql.Append(" where ClassID='" + ClassId + "'");
            return MysqlHelper.Query(conn, strSql.ToString());
        }

        /// <summary>
        /// 添加一个菜单项
        /// </summary>
        /// <param name="ClassId"></param>
        /// <param name="ClassName"></param>
        /// <param name="ClassList"></param>
        /// <param name="ClassPre"></param>
        /// <param name="ClassTj"></param>
        /// <returns></returns>
        public long ClassAdd(MaterialModel model)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into ")
    .Append("tbl_product_class")
    .Append("(ClassId, ClassName, ClassList, ClassPre, ")
    .Append("ClassTj, ClassKind)")

    .Append("values")

    .Append("(@ClassId, @ClassName, @ClassList, @ClassPre, ")
    .Append("@ClassTj, @ClassKind)");

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@ClassId",MySqlDbType.VarChar,200),
                    new MySqlParameter("@ClassName",MySqlDbType.VarChar,200),
                    new MySqlParameter("@ClassList",MySqlDbType.VarChar,900),
                    new MySqlParameter("@ClassPre",MySqlDbType.VarChar,200),
                    new MySqlParameter("@ClassTj",MySqlDbType.Int32),
                    new MySqlParameter("@ClassKind",MySqlDbType.Int32)
                };
            ps[0].Value = model.ClassId;
            ps[1].Value = model.ClassName;
            ps[2].Value = model.ClassList;
            ps[3].Value = model.ClassPre;
            ps[4].Value = model.ClassTj;
            ps[5].Value = model.ClassKind;

            return MysqlHelper.Insert(conn, strSql.ToString(), ps);
        }

        /// <summary>
        /// 编辑一个菜单项
        /// </summary>
        /// <param name="ClassId"></param>
        /// <param name="ClassName"></param>
        /// <param name="ClassList"></param>
        /// <param name="ClassPre"></param>
        /// <param name="ClassTj"></param>
        /// <returns></returns>
        public long ClassSave(MaterialModel model)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbl_product_class set ");
            strSql.Append("ClassName=@ClassName,");
            strSql.Append("ClassList=@ClassList,");
            strSql.Append("ClassPre=@ClassPre,ClassTj=@ClassTj");
            strSql.Append(" where ClassId=@ClassId ");


            //同步更新子菜单项
            DataSet ds = this.GetSubClassList(model.ClassId);
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    string SubClassList = model.ClassList + dr["ClassId"].ToString().Trim() + ",";

                    model.ClassId = dr["ClassId"].ToString().Trim();
                    model.ClassName = dr["ClassName"].ToString().Trim();
                    model.ClassList = SubClassList.ToString().Trim();
                    model.ClassPre = dr["ClassPre"].ToString().Trim();
                    model.ClassTj = model.ClassTj + 1;
                    ClassSave(model);
                }
            }

            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@ClassName",MySqlDbType.VarChar,200),
                    new MySqlParameter("@ClassList",MySqlDbType.VarChar,900),
                    new MySqlParameter("@ClassPre",MySqlDbType.VarChar,200),
                    new MySqlParameter("@ClassTj",MySqlDbType.Int32)
                };
            ps[0].Value = model.ClassName;
            ps[1].Value = model.ClassList;
            ps[2].Value = model.ClassPre;
            ps[3].Value = model.ClassTj;


            return MysqlHelper.Update(conn, strSql.ToString(), ps);
        }

        /// <summary>
        /// 获取该菜单项的所有子菜单项
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public DataSet GetSubClassList(string ClassId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tbl_product_class");
            strSql.Append(" where ClassPre='" + ClassId + "' ");
            return MysqlHelper.Query(conn, strSql.ToString());
        }
        public List<ProductClassModel> GetSubClassList(StringBuilder ClassId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tbl_product_class");
            strSql.Append(" where ClassPre='" + ClassId + "' ");
            DataSet ds = MysqlHelper.Query(conn, strSql.ToString());
            List<ProductClassModel> list = new List<ProductClassModel>();
            if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    ProductClassModel m = new ProductClassModel();
                    m.ClassId = Convert.ToString(row["ClassId"]);
                    m.ClassList = Convert.ToString(row["ClassList"]);
                    m.ClassName = Convert.ToString(row["ClassName"]);
                    m.ClassPre = Convert.ToString(row["ClassPre"]);
                    list.Add(m);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetClassList(int ClassKind)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from tbl_product_class");
            strSql.Append(" where ClassKind=" + ClassKind + "");
            strSql.Append(" Order By ClassList Asc,ClassOrder Asc");
            return MysqlHelper.Query(conn, strSql.ToString());
        }

        public string GetPreClassId(string ClassId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("Select ClassPre From tbl_product_class");
            strSql.Append(" Where ClassId='" + ClassId + "'  LIMIT 0,1");
            return MysqlHelper.ExecuteScalar(conn, strSql.ToString()).ToString();
        }

        /// <summary>
        /// 删除一个菜单项
        /// </summary>
        /// <param name="ClassId"></param>
        /// <returns></returns>
        public int DelByClassId(string ClassId)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;

            StringBuilder strSql = new StringBuilder();
            strSql.Append("Delete From tbl_product_class");
            strSql.Append(" where ClassId=@ClassId");
            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@ClassId",MySqlDbType.VarChar,200)
                };
            ps[0].Value = ClassId;
            return MysqlHelper.Delete(conn, strSql.ToString(), ps);
        }

        /// <summary>
        /// 更新排序
        /// </summary>
        /// <param name="ClassId"></param>
        /// <param name="ClassOrder"></param>
        /// <returns></returns>
        public bool UpdateClassOrder(string ClassId, int ClassOrder)
        {
            bool flag;
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;


            StringBuilder strSql = new StringBuilder();
            strSql.Append("Update tbl_product_class Set ");
            strSql.Append("ClassOrder=@ClassOrder ");
            strSql.Append(" where ClassId=@ClassId");
            MySqlParameter[] ps = new MySqlParameter[] {
                    new MySqlParameter("@ClassId",MySqlDbType.Int32)
                };
            ps[0].Value = ClassId;
            ps[1].Value = ClassOrder;
            if (MysqlHelper.Update(conn, strSql.ToString(), ps) > 0)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }

            return flag;
        }

        /// <summary>
        /// 通过ClassId获得className
        /// </summary>
        /// <param name="classid"></param>
        /// <returns></returns>
        public string GetClassNameByClassId(string classid)
        {
            String conn = System.Configuration.ConfigurationManager.ConnectionStrings["MySqlHost"].ConnectionString;
            string sql = "select ClassName from tbl_product_class where classid='"+classid+"'";

            return MysqlHelper.ExecuteScalar(conn,sql).ToString();
        }
        #endregion
    }
}
